package cn.mumzone.pos.ui.adapter;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.TimeCardEntity;
import cn.mumzone.pos.ui.widgets.shopping.AnimShopButton;

/**
 * Created by qiuzhenhuan on 2019/7/2.
 */

public class ADA_TimesCard extends CommonAdapter<TimeCardEntity> {

    private List<TimeCardEntity> mSelectedItemList;

    private boolean isDoubleCheck = true;

    public List<TimeCardEntity> getSelectedItemList() {

        return ListUtil.deepCopy(mSelectedItemList);
    }

    public ADA_TimesCard(Context context) {
        super(context);
        this.mSelectedItemList = new ArrayList<>();
    }


    public void clearCheckStatus() {
        for (TimeCardEntity bean : mSelectedItemList) {
            bean.setCardNum(0);
        }
        mSelectedItemList.clear();
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_dialog_settle_timecard;
    }

    @Override
    protected void convert(ViewHolder holder, final TimeCardEntity timeCardEntity, int position) {
        // 设置选中
        TimeCardEntity bean = checkTimeCardIsSelected(timeCardEntity);
//        holder.setVisible(R.id.iv_selected, null != bean);

        if (null == bean) {
            timeCardEntity.setCardNum(0);
        } else {
            timeCardEntity.setCardNum(bean.getCardNum());
        }

        // 设置次卡信息
        holder.setText(R.id.tv_card_name, StringUtils.get(timeCardEntity.getGoodsName()));
        holder.setText(R.id.tv_card_des, StringUtils.get(timeCardEntity.getServiceName()));
        holder.setText(R.id.tv_card_use_info, String.format("%s/%s", StringUtils.get(timeCardEntity.getResidueTimes()),
                StringUtils.get(timeCardEntity.getTimes())));

        holder.setText(R.id.tv_card_last_time, String.format("有效期至：%s",
                AppDateUtil.getTimeStamp(timeCardEntity.getEndDate(), AppDateUtil.YYYY_MM_DD2)));

        AnimShopButton shopButton = holder.getView(R.id.asb_count);
        shopButton.setCount(timeCardEntity.getCardNum());
        shopButton.setMaxCount(timeCardEntity.getResidueTimes());
        if (onCountChangeListener != null) {
            onCountChangeListener.onCountChange();
        }
        shopButton.setOnAddDelListener(new GoodsShopButtonAdapter(mContext) {
            @Override
            public void onAddSuccess(int count) {
                if (timeCardEntity.getCardNum() == count) {
                    return;
                }
                TimeCardEntity bean = checkTimeCardIsSelected(timeCardEntity);
                timeCardEntity.setCardNum(count);
                if (null == bean) {
                    mSelectedItemList.add(timeCardEntity);
                } else {
                    bean.setCardNum(count);
                }
                notifyDataSetChanged();
                if (onCountChangeListener != null) {
                    onCountChangeListener.onCountChange();
                }
            }

            @Override
            public void onAddFailed(int count, FailType failType) {

            }

            @Override
            public void onDelSuccess(int count) {
                TimeCardEntity bean = checkTimeCardIsSelected(timeCardEntity);
                timeCardEntity.setCardNum(count);
                if (null != bean) {
                    if (count == 0) {
                        mSelectedItemList.remove(bean);
                    } else {
                        bean.setCardNum(count);
                    }
                }
                notifyDataSetChanged();
                if (onCountChangeListener != null) {
                    onCountChangeListener.onCountChange();
                }
            }

            @Override
            public void onDelFailed(int count, FailType failType) {

            }

            @Override
            public void onInputAreaClicked() {

            }
        });

    }


    private TimeCardEntity checkTimeCardIsSelected(TimeCardEntity timesCard) {
        for (TimeCardEntity bean : mSelectedItemList) {
            if (bean.getId().equals(timesCard.getId())) {
                return bean;
            }
        }
        return null;
    }

    public void setSelectedItemList(List<TimeCardEntity> mSelectedItemList) {
        this.mSelectedItemList = mSelectedItemList;
        notifyDataSetChanged();
    }

    private OnCountChangeListener onCountChangeListener;

    public void setOnCountChangeListener(OnCountChangeListener onCountChangeListener) {
        this.onCountChangeListener = onCountChangeListener;
    }

    public interface OnCountChangeListener {
        void onCountChange();
    }
}
